package com.whu.helpluojia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.helpluojia.domain.*;
import com.whu.helpluojia.service.SupplyService;
import com.whu.helpluojia.dao.SupplyDao;
import com.whu.helpluojia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 92948
* @description 针对表【supply】的数据库操作Service实现
*/
@Service
public class SupplyServiceImpl extends ServiceImpl<SupplyDao, Supply> implements SupplyService{
    @Autowired
    SupplyDao supplyDao;

    @Autowired
    UserService userService;


    /**
     * 增加供需帖子
     * @param supply
     * @return
     */
    @Override
    public int addSupply(Supply supply){
        return supplyDao.insert(supply);
    }

    /**
     * 根据id删除供需帖子
     * @param id
     * @return
     */
    @Override
    public int deleteSupply(Long id) {
        return supplyDao.deleteById(id);
    }

    /**
     * 根据id修改供需帖子
     * @param supply
     * @return
     */
    @Override
    public int updateSupply(Supply supply){
        return supplyDao.updateById(supply);
    }

    /**
     * 根据id查询供需帖子
     * @param id
     * @return
     */
    @Override
    public Supply querySupplyById(Long id){
        return supplyDao.selectById(id);
    }

    /**
     * 查询所有供需帖子
     * @return
     */
    @Override
    public List<Supply> querySupplyList(){
        return supplyDao.selectList(null);
    }

    /**
     * 根据供需帖子的发布时间查询供需帖子
     * @param dateTime
     * @return
     */
    @Override
    public List<Supply> querySupplyByDate(Date dateTime){
        LambdaQueryWrapper<Supply> lqw = new LambdaQueryWrapper<Supply>();
        lqw.eq(Supply::getTime,dateTime);
        return supplyDao.selectList(lqw);
    }

    /**
     * 根据发帖人的用户id查询供需帖子
     * @param userId
     * @return
     */
    @Override
    public Supply querySupplyByUser(Long userId){
        return supplyDao.selectById(userId);
    }

    /**
     * 根据所选供需的种类查询供需帖子
     * @param category
     * @return
     */
    @Override
    public List<Supply> querySupplyByCategory(String category){
        LambdaQueryWrapper<Supply> lqw = new LambdaQueryWrapper<Supply>();
        lqw.like(Supply::getType,category);
        return supplyDao.selectList(lqw);
    }

    /**
     * 根据描述片段查询供需帖子
     * @param description
     * @return
     */
    @Override
    public List<Supply> querySupplyByDescription(String description){
        LambdaQueryWrapper<Supply> lqw = new LambdaQueryWrapper<Supply>();
        lqw.eq(Supply::getDescription,description);
        return supplyDao.selectList(lqw);
    }

    @Override
    public void solute(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", 1);
        supplyDao.update(null, updateWrapper);
    }

    @Override
    public MyPublish getSupplyDetail(Long id, Long userId) {

        Supply supply=supplyDao.selectById(id);

        User user=userService.selectUser(userId);

        MyPublish myPublish=new MyPublish();

        myPublish=copyData(myPublish,supply,user);

        return myPublish;
    }

    /**
     * 根据定位查询供需帖子
     * @param
     * @param
     * @param
     * @param
     * @param
     * @return
     *//*
    @Override
    public List<Supply> querySupplyByDestination(Supply supply, double maxLat, double maxLon, double minLat, double minLon){
        SupplyQuery sq = new SupplyQuery();
        sq.setMaxLat(maxLat);
        sq.setMaxLon(maxLon);
        sq.setMinLat(minLat);
        sq.setMinLon(minLon);
        //null判定
        //先判定第一个参数是不是true，如果为true就链接当前条件
        LambdaQueryWrapper<Supply> lqw = new LambdaQueryWrapper<Supply>();
        lqw.lt(null !=sq.getMaxLat(),Supply::getLat,sq.getMaxLat());
        lqw.lt(null !=sq.getMaxLon(),Supply::getLon,sq.getMaxLon());
        lqw.gt(null !=sq.getMinLat(),Supply::getLat,sq.getMinLat());
        lqw.gt(null !=sq.getMinLon(),Supply::getLon,sq.getMinLon());
        //如果是或关系的话就在中间加“.or()”
        List<Supply> supplyList = supplyDao.selectList(lqw);
        return supplyDao.selectList(lqw);
    }*/

    @Override
    public List<MyPublish> getMyDetail(Long userId) {
        LambdaQueryWrapper<Supply> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Supply::getUserId,userId);
        List<Supply> findList=supplyDao.selectList(lambdaQueryWrapper);

        User user=userService.selectUser(userId);

        List<MyPublish> myPublishes=new ArrayList<>();

        for(Supply supply:findList){
            MyPublish myPublish=new MyPublish();
            myPublish=copyData(myPublish,supply,user);
            myPublishes.add(myPublish);
        }

        return myPublishes;
    }

    @Override
    public Supply[] selectAll() {
        List<Supply> supplies = supplyDao.selectList(null);
        return supplies.toArray(new Supply[0]);
    }

    public MyPublish copyData(MyPublish myPublish,Supply supply,User user){
        myPublish.setId(supply.getId().longValue());
        myPublish.setCategory("物品供需");
        myPublish.setContent(supply.getDescription());
        myPublish.setSex(user.getGender());
        myPublish.setStatus(supply.getStatus());
        myPublish.setSenderName(user.getName());
        myPublish.setTime(supply.getTime());
        myPublish.setType(supply.getType());
        return myPublish;

    }
}
